var ns, toast, dialog;
var app = new Vue({
    el: '#app',
    data: {
        ID:0,//工单ID
        form:{
            keywords:'',
        },
        //价格列表数据
        allpricelist: [],
        pricelist: [],
        selectIDs:[],
    },
    methods: {
        dochooseselect(item){
        },
        get_data: function() {
            var that = this;
            var options = {};
            options.ID = that.ID;    
            options.action = "getnewservicedetail";
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data))
                if (succeed) {
                   //that.form = data.form;
                   that.get_dataproject(data.form.ProjectID);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        get_dataproject: function (ProjectID) {
            var that = this;
            var options = {};
            options.action = "getservicechargelist";
            options.ProjectID=ProjectID;
            // console.log(JSON.stringify(options))
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data))
                if (succeed) {
                    if (data.list.length > 0 && that.selectIDs.length > 0) {
                        for (var i = 0; i < data.list.length; i++) {
                            data.list[i].IsSelected = false;
                            var findIndex = that.selectIDs.findIndex(p => p === data.list[i].ID);
                            if (findIndex > -1) {
                                data.list[i].IsSelected = true;
                            }
                        }
                    }
                    that.allpricelist = data.list;
                    that.pricelist = data.list;
                   
                } else if (err) {
                    ns.toast(err);
                }
            });
        },
        onPriceSearch: function () {
            var that = this;
            if (that.form.keywords.trim() != '') {
                let arr=that.allpricelist.filter(e => {
                    return e.ItemName.trim().indexOf(that.form.keywords.trim()) > -1;
                  });
                that.pricelist = arr;
            }
            else {
                that.pricelist = that.allpricelist;
            }
        },
        //返回
        onReturn: function (item) {
            var that = this;
            api.closeWin();
        },
        //确认选择
        onChoose(){
            var that=this;
            var chooselist=[];
            if(that.pricelist.length>0){
                that.pricelist.forEach(function(item) {
                   if(item.IsSelected){
                    chooselist.push(item);
                   }
                })
            }
            if(chooselist.length<=0){
                api.toast({
                    msg: '请选择维修项目',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.sendEvent({
                name:'do_loadprice_list',
                extra: {
                    data: chooselist,
                }
            })
            api.closeWin();
        },
    }
});

apiready = function () {
    dialog = new auiDialog();
    ns = window.Foresight.Util;
    app.ID = api.pageParam.id || 0;
    app.selectIDs= api.pageParam.selectIDs || [];
    app.get_data();
}
